package com.zqxq.lottery.controller.user;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.zqxq.common.constant.Result;
import com.zqxq.common.enums.CodeEnum;
import com.zqxq.common.utils.StrUtils;
import com.zqxq.lottery.entity.o2o.user.UserAlipayO2O;
import com.zqxq.lottery.entity.po.user.UserAlipayPO;
import com.zqxq.lottery.entity.po.user.UserInfoPO;
import com.zqxq.lottery.entity.vo.user.UserAlipayVO;
import com.zqxq.lottery.service.user.IUserAlipayService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@Slf4j
@RequestMapping("/alipay")
public class UserAlipayController extends UserBaseController{

    @Autowired
    private IUserAlipayService userAlipayService;

    @ApiOperation("绑定支付宝")
    @PostMapping("/bindAlipay")
    public Result bindAlipay(Long userId, String alipayAccount, String openId, Integer sex, String city){
        Result result = new Result();
        if(userId == null || StrUtils.isEmpty(alipayAccount) || StrUtils.isEmpty(openId)){
            return new Result(CodeEnum.FAIL_100002.getCode(), CodeEnum.FAIL_100002.getMsg());
        }
        try {
            UserInfoPO userInfoPO = new UserInfoPO();
            userInfoPO.setId(userId);
            userInfoPO.setCity(city);
            userInfoPO.setSex(sex);
            userAlipayService.bindAlipay(userId, alipayAccount, openId, userInfoPO);
        } catch (Exception e) {
            log.error("alipay bindAlipay is error!", e);
            result.setCode(CodeEnum.FAIL_100005.getCode());
            result.setMsg(CodeEnum.FAIL_100005.getMsg());
        }
        return result;
    }

    @ApiOperation("获取用户支付宝绑定关系")
    @PostMapping("/queryUserAlipay")
    public Result<List<UserAlipayVO>> queryUserAlipay(String alipayAccount){
        Result result = new Result();
        if(StrUtils.isEmpty(alipayAccount)){
            return new Result(CodeEnum.FAIL_100002.getCode(), CodeEnum.FAIL_100002.getMsg());
        }
        try {
            Wrapper wrapper = new EntityWrapper();
            wrapper.eq("alipay_account", alipayAccount);
            List<UserAlipayPO> poList= userAlipayService.selectList(wrapper);
            List<UserAlipayVO> voList = UserAlipayO2O.toVOList(poList);
            result.setData(voList);
        } catch (Exception e) {
            log.error("alipay queryUserAlipay is error!", e);
            result.setCode(CodeEnum.FAIL_100005.getCode());
            result.setMsg(CodeEnum.FAIL_100005.getMsg());
        }
        return result;
    }

    /**
    * @Author 苦智
    * @Description 校验用户是否绑定支付宝
    * @Date 10:22 2018/10/16
    * @Param [userId]
    * @return com.zqxq.common.constant.Result
    **/
    @PostMapping("/validateUserAlipay")
    public Result validateUserAlipay(String userId){
        if (StrUtils.isEmpty(userId))
            return new Result(CodeEnum.FAIL_100002.getCode(),CodeEnum.FAIL_100002.getMsg());
        UserAlipayPO alipayPO = userAlipayService.selectById(userId);
        if(alipayPO == null || StrUtils.isEmpty(alipayPO.getAlipay_account())){
            return new Result(10008,"请绑定支付宝账号！");
        }
        return new Result(CodeEnum.SUCCESS.getCode(),CodeEnum.SUCCESS.getMsg());
    }

}
